package org.zpmis.domain.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.zpmis.common.utils.HttpUtils;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.domain.user.service.WxApiService;
import org.zpmis.config.WxConfig;

@Service
@Slf4j
public class WxApiServiceImpl implements WxApiService {

    @Autowired
    private WxConfig wxConfig;
    @Override
    public String getWxOpenIdByCode(String code) {
        String param = "appid="+ wxConfig.appid+"&secret="+wxConfig.secret+"&js_code="+code+"&grant_type=authorization_code";
        String openId = null;
        try {
            //根据appCode 获取用户微信openId
            String result = HttpUtils.sendGet(wxConfig.url, param);
            log.info("[获取小程序openid返回结果]---result:{}",result);
            if(StringUtil.isEmpty(result)){
                return null;
            }
            JSONObject resObj = JSONObject.parseObject(result);
            openId = resObj.getString("openid");
        } catch (Exception e) {
            log.error("[获取小程序openid返回结果异常]---e:",e);
            return null;
        }
        return openId;
    }
}
